package com.goods.controller.business;

import com.goods.business.service.InStockService;
import com.goods.business.service.SupplierService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "入库模块-入库相关业务接口")
@RestController
@RequestMapping("/business/inStock")
public class InStockController {

  @Autowired
  private InStockService inStockService;

  @Autowired
  private SupplierService supplierService;

  @ApiOperation(value = "入库单（入库记录）",notes = "数据分页列表展示")
  @GetMapping("/findInStockList")
  public ResponseBean findInStockList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize") Integer pageSize,
                                      InStockVO inStockVO){
    PageVO<InStockVO> inStockVOPageVO = inStockService.findInStockList(pageNum,pageSize,inStockVO);
    return ResponseBean.success(inStockVOPageVO);
  }

  @ApiOperation(value = "入库明细",notes = "点击明细时，展示该入库单对应的入库物资列表")
  @GetMapping("/detail/{id}")
  public ResponseBean detail(@PathVariable Long id,
                             @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize) throws BusinessException {
    InStockDetailVO inStockDetailVO = inStockService.detail(id,pageNum,pageSize);
    return ResponseBean.success(inStockDetailVO);
  }

  @ApiOperation(value = "添加入库信息")
  @PostMapping("/addIntoStock")
  public ResponseBean addIntoStock(@RequestBody @Validated InStockVO inStockVO) throws BusinessException {
    if (inStockVO.getSupplierId()==null){
      SupplierVO supplierVO = new SupplierVO();
      BeanUtils.copyProperties(inStockVO,supplierVO);
      if ("".equals(supplierVO.getName())||supplierVO.getName()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资来源名称不能为空");
      }
      if ("".equals(supplierVO.getName())||supplierVO.getEmail()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"邮箱不能为空");
      }
      if ("".equals(supplierVO.getName())||supplierVO.getContact()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"联系人不能为空");
      }
      if ("".equals(supplierVO.getName())||supplierVO.getAddress()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"地址不能为空");
      }
      if ("".equals(supplierVO.getName())||supplierVO.getPhone()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"联系方式不能为空");
      }
      if (supplierVO.getSort()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"排序不能为空");
      }
      Supplier supplier = supplierService.add(supplierVO);
      inStockVO.setSupplierId(supplier.getId());
    }
    inStockService.addIntoStock(inStockVO);
    return ResponseBean.success();
  }

  @ApiOperation(value = "入库信息移动至回收站")
  @PutMapping("/remove/{id}")
  public ResponseBean remove(@PathVariable Long id) throws BusinessException {
    inStockService.remove(id);
    return ResponseBean.success();
  }

  @ApiOperation(value = "恢复入库单")
  @PutMapping("/back/{id}")
  public ResponseBean back(@PathVariable Long id) throws BusinessException {
    inStockService.back(id);
    return ResponseBean.success();
  }

  @ApiOperation(value = "审核通过入库单")
  @PutMapping("/publish/{id}")
  public ResponseBean publish(@PathVariable Long id) throws BusinessException {
    inStockService.publish(id);
    return ResponseBean.success();
  }

  @ApiOperation(value = "删除入库单",notes = "只有回收站和待审核中的入库单才能被删除")
  @GetMapping("/delete/{id}")
  public ResponseBean delete(@PathVariable Long id) throws BusinessException {
    inStockService.delete(id);
    return ResponseBean.success();
  }
}
